/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVASE Song Huongkang PROJECT
 * All Rights Reserved.
 */

package 练习.多窗口售票;

public class WindowsTest {
    public static void main(String[] args) {

        Windows windows1 = new Windows("窗口一");
        Windows windows2 = new Windows("窗口二");
        Windows windows3 = new Windows("窗口三");

        windows1.start();
        windows2.start();
        windows3.start();
    }
}
class Windows extends Thread{
    private static int ticket = 1000;// 不加static会分别使用
//    private static final Object object = new Object();
    // static会导致线程不安全

    public Windows(String name) {
        super(name);
    }

    @Override
    public void run() {
        synchronized (Windows.class){// 不可以是this
            while(true){
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (ticket>0){
                    System.out.println(getName()+"售票 x1 号码为："+ticket);
                    ticket--;
                }else{
                    break;
                }
            }
        }

    }
}